﻿//---------------------------------------------------------------------------------
// Copyright 2011 Microsoft Corporation
// Licensed under the Microsoft Public License (MS-PL) (the "License"); 
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.opensource.org/licenses/ms-pl.html
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
// INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
// CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
// MERCHANTABLITY OR NON-INFRINGEMENT. 
// See the Microsoft Public License (MS-PL) for the specific language governing 
// permissions and limitations under the License.
//---------------------------------------------------------------------------------

using System;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Resources;

namespace Microsoft.Telco.Controls.WindowsLive
{
    /// <summary>
    /// 
    /// </summary>
    public static class ResourceHelper
    {
        static ResourceHelper()
        {
            CommonStyles = LoadResourceDictionary("Common/CommonStyles.xaml");
        }

        /// <summary>
        /// Gets or sets the common styles.
        /// </summary>
        /// <value>The resource dictionary containing common styles.</value>
        public static ResourceDictionary CommonStyles { get; set; }

        /// <summary>
        /// Loads the resource dictionary.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static ResourceDictionary LoadResourceDictionary(string relativePath)
        {
            string assemblyShortName = Assembly.GetExecutingAssembly().FullName.Split(new char[] { ',' })[0];
            string path = string.Format("{0};component/{1}", assemblyShortName, relativePath);
            StreamResourceInfo sr = Application.GetResourceStream(new Uri(path, UriKind.Relative));
            StreamReader reader = new StreamReader(sr.Stream);
            string xaml = reader.ReadToEnd();
            return (ResourceDictionary)XamlReader.Load(xaml);
        }
    }
}
